package com.example.mymvvmdemo.models.main.fragment.notification;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseLoadFragment;
import com.example.mymvvmdemo.constants.UrlConstants;
import com.example.mymvvmdemo.constants.UrlTypeConstants;
import com.example.mymvvmdemo.databinding.FragmentHomeBinding;
import com.example.mymvvmdemo.databinding.FragmentNotificationBinding;
import com.example.mymvvmdemo.entity.FailEntity;
import com.example.mymvvmdemo.models.main.MainViewModel;
import com.example.mymvvmdemo.models.main.fragment.device.adapter.DeviceAdapter;
import com.example.mymvvmdemo.models.main.fragment.home.entity.BlogArticlesEntity;
import com.example.mymvvmdemo.models.main.fragment.notification.adapter.NotificationAdapter;
import com.example.mymvvmdemo.models.main.fragment.notification.entity.MessageMessagesEntity;
import com.example.mymvvmdemo.net.presenter.GetPresenter;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.LogUtils;
import com.example.mymvvmdemo.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class NotificationFragment extends BaseLoadFragment<FragmentNotificationBinding> implements IView.GetView, NotificationAdapter.MessageListener {

    private MainViewModel mainViewModel;
    private GetPresenter getPresenter;
    private NotificationAdapter notificationAdapter;

    @Override
    protected void initData() {
        getPresenter = new GetPresenter(this);
        initAdapter();
        startLoadMessage();
    }

    @Override
    protected void initListener() {
        binding.refreshViewShowContent.setOnRefreshListener(relayout -> {
            deviceComplete();
            startLoadMessage();
        });
        binding.refreshViewShowContent.setOnLoadMoreListener(relayout -> {
            deviceComplete();
            startLoadMessage();
        });
    }

    /****************************************自定义方法*******************************************/
    public void setMainViewModel(MainViewModel mainViewModel) {
        this.mainViewModel = mainViewModel;
    }

    private void deviceComplete() {
        try {
            binding.refreshViewShowContent.finishRefresh();
            binding.refreshViewShowContent.finishLoadMore();
            notificationAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            LogUtils.e("deviceComplete error:" + e.getMessage());
        }
    }

    private void initAdapter() {
        notificationAdapter = new NotificationAdapter(getContext(), mainViewModel.notificationList);
        notificationAdapter.setListener(this);
        binding.recyclerView.setAdapter(notificationAdapter);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    public void startLoadMessage() {
        showDialog();
        HashMap<String, String> hashMap = new HashMap<>();
        getPresenter.startLoadGet(UrlConstants.messageMessages, hashMap, UrlTypeConstants.messageMessages);
    }

    public void showNoDataView() {
        int dataListSize = notificationAdapter.getDataListSize();
        binding.includeNoData.ivNoData.setVisibility(dataListSize == 0 ? View.VISIBLE : View.GONE);
    }

    /******************************数据请求回调************************************/
    @Override
    public void loadGetSuccess(String result, String type, int code) {
        if (type.equals(UrlTypeConstants.messageMessages)) {
            loadMessageMessagesSuccess(result, code);
        }
    }

    @Override
    public void loadGetFail(String message, String type) {
        if (type.equals(UrlTypeConstants.messageMessages)) {
            loadMessageMessagesFail();
        }
    }

    @Override
    public void loadGetAccountError(int code) {

    }

    @Override
    public void loadGetNetWorkError() {
        dismissDialog();
        toast(R.string.network_error, ToastUtils.TOA_W);
    }


    /**************************************处理请求后数据***********************************************/
    private void loadMessageMessagesSuccess(String result, int code) {

        dismissDialog();
        if (code != 200) {
            FailEntity registerFailEntity = gson.fromJson(result, FailEntity.class);
            String message = registerFailEntity.getData().getMessage().getMessages().get(0);
            toast(message, ToastUtils.TOA_W);
            loadMessageMessagesFail();
            return;
        }

        try {
            MessageMessagesEntity messageMessages = gson.fromJson(result, MessageMessagesEntity.class);
            MessageMessagesEntity.DataDTO data = messageMessages.getData();

            if (data == null) {
                loadMessageMessagesFail();
                return;
            }

            ArrayList<MessageMessagesEntity.DataDTO.ResultsDTO> results = data.getResults();
            if (results.size() == 0) {
                loadMessageMessagesFail();
                return;
            }
            mainViewModel.notificationList.clear();
            mainViewModel.notificationList.addAll(results);
            notificationAdapter.notifyDataSetChanged();
            showNoDataView();
            deviceComplete();

        } catch (Exception e) {
            loadMessageMessagesFail();
        }
    }

    private void loadMessageMessagesFail() {
        deviceComplete();
        showNoDataView();
        dismissDialog();
    }

    /*****************************************列表点击事件**********************************/
    @Override
    public void onClickMessage(int position) {

        try {
            MessageMessagesEntity.DataDTO.ResultsDTO resultsDTO = notificationAdapter.getDataList().get(position);
            mainViewModel.startMessageDetailActivity(getActivity(), resultsDTO.getId());
        } catch (Exception e) {
        }

    }

}
